import Vue from "vue";
import VueRouter from "vue-router";
import { Toast } from "vant";
// 解决路由 点击多次报错
const originalPush = VueRouter.prototype.push;
   VueRouter.prototype.push = function(location) {
   return originalPush.call(this, location).catch(err =>{})
}
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import(/* webpackChunkName: "home" */ "../views/Home.vue"),
    meta: {
      showTabber: true,
    },
    children: [
      {
        path: "/home/searchpopup",
        name: "popup",
        component: () =>
          import(/* webpackChunkName: "popup" */ "../views/searchPopup.vue"),
      },
    ],
  },
  {
    path: "/home/iconDetail/:id/:i",
    name: "iconDetail",
    component: () =>
      import(
        /* webpackChunkName: "iconDetail" */ "../components/iconDetail.vue"
      ),
  },
  {
    path: "/subject",
    name: "subject",
    meta: {
      showTabber: true,
    },
    component: () =>
      import(/* webpackChunkName: "subject" */ "../views/Subject.vue"),
  },
  {
    path: "/cate",
    name: "cate",
    meta: {
      showTabber: true,
    },
    component: () => import(/* webpackChunkName: "cate" */ "../views/Cate.vue"),
  },
  {
    path: "/car",
    name: "car",
    meta: {
      showTabber: true,
    },
    component: () => import(/* webpackChunkName: "car" */ "../views/Car.vue"),
  },

  {
    path: "/my",
    name: "my",
    meta: {
      showTabber: true,
    },
    component: () => import(/* webpackChunkName: "car" */ "../views/My.vue"),
  },

  {
    path: "/goodsDetail/:id",
    name: "goodsDetail",
    component: () =>
      import(
        /* webpackChunkName: "goodsDetail" */ "../components/goodsDetail.vue"
      ),
  },
  {
    path: "/brand/:id",
    name: "brand",
    component: () =>
      import(/* webpackChunkName: "brand" */ "../components/brand.vue"),
  },
  {
    path: "/404not",
    name: "404not",
    component: () =>
      import(/* webpackChunkName: "404not" */ "../components/404not.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// 路由守卫(路由拦截)
// router.beforeEach((to,from,next)=>{
// to参数表示即将要去的路由  console.log("to:",to);
//from 从哪里来
// console.log("from:",from);
// console.log("next:",next);
// })
router.beforeEach((to, from, next) => {
  //to表示要去的路由
  //from表示要从哪来
  //next中的参数要去那里的路由
  // next("")
  if (to.path == "/car") {
    let token = localStorage.getItem("token");
    if (token) {
      next();
    } else {
      Toast.loading({
        message: "请先登录...",
        forbidClick: true,
        duration: 500,
      });
      next("/my");
    }
    return;
  }
  next();
});
export default router;
